﻿using ICSharpCode.SharpZipLib.Zip;

namespace WCS.Tools
{
    public class FileZipHelper
    {
        public static class ZipHelper
        {
            /// <summary>
            /// 压缩文件
            /// </summary>
            /// <param name="sourceFilePath"></param>
            /// <param name="destinationZipFilePath"></param>
            public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
            {


                var fileName = Path.GetFileNameWithoutExtension(sourceFilePath);

                FileStream streamToZip = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read);
                FileStream zipFile = File.Create(destinationZipFilePath);
                ZipOutputStream zipStream = new ZipOutputStream(zipFile);

                ZipEntry zipEntry = new ZipEntry(fileName);
                zipStream.PutNextEntry(zipEntry);

                //存储、最快、较快、标准、较好、最好  0-9
                zipStream.SetLevel(6);

                byte[] buffer = new byte[1024];

                int size = streamToZip.Read(buffer, 0, buffer.Length);
                zipStream.Write(buffer, 0, size);
                try
                {
                    while (size < streamToZip.Length)
                    {
                        int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                        zipStream.Write(buffer, 0, sizeRead);
                        size += sizeRead;
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                zipStream.Finish();
                zipStream.Close();
                streamToZip.Close();
            }

            /// <summary>
            /// 递归压缩文件
            /// </summary>
            /// <param name="sourceFilePath">待压缩的文件或文件夹路径</param>
            /// <param name="zipStream">
            /// <param name="staticFile"></param>
            private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile)
            {

                string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
                foreach (string file in filesArray)
                {
                    if (Directory.Exists(file))                     //如果当前是文件夹，递归
                    {
                        CreateZipFiles(file, zipStream, staticFile);
                    }

                    else                                            //如果是文件，开始压缩
                    {
                        FileStream fileStream = File.OpenRead(file);

                        byte[] buffer = new byte[fileStream.Length];
                        fileStream.Read(buffer, 0, buffer.Length);
                        string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                        ZipEntry entry = new ZipEntry(tempFile);

                        entry.DateTime = DateTime.Now;
                        entry.Size = fileStream.Length;
                        fileStream.Close();
                        zipStream.PutNextEntry(entry);

                        zipStream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
        }


    }
}
